import React, {Component} from 'react';
import {
  AppRegistry,
  Dimensions,
  StyleSheet,
  Text,
  TouchableHighlight,
  View
} from 'react-native';
import {Actions} from 'react-native-router-flux'
import Camera from 'react-native-camera';

export default class BadInstagramCloneApp extends Component {
  render() {
    let thisPop = '< 返回'
    return (
      <View style={styles.container}>
        <Camera
          ref={(cam) => {
            this.camera = cam;
          }}
          onBarCodeRead={this.onBarCodeRead.bind(this)}
          style={styles.preview}
          aspect={Camera.constants.Aspect.fill}
          captureQuality={Camera.constants.CaptureQuality.medium}
        >
          <View style={{flexDirection:'row'}}>
            <Text style={styles.capture} onPress={() =>{Actions.pop()}}>{thisPop}</Text>
            <Text style={styles.capture} onPress={this.takePicture.bind(this)}>[拍照]</Text>
          </View>
        </Camera>
      </View>
    );
  }

  onBarCodeRead(e) {
    console.log(
      "Barcode Found!",
      "Type: " + e.type + "\nData: " + e.data
    );
  }

  takePicture() {
    const {saveSuccess, upload, photoType, photoAction} = this.props
    const options = {};
    //options.location = ...
    this.camera.capture({metadata: options})
      .then((data) => {
        console.log(options,data)
        upload({type: photoType, uri: data.path, name: 'test.jpg', photoAction: photoAction})
        // Actions.push('RectifyBill')
      })
      .catch(err => console.error(err));
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: 'row',
  },
  preview: {
    flex: 1,
    justifyContent: 'flex-end',
    alignItems: 'center'
  },
  capture: {
    flex: 0,
    backgroundColor: '#fff',
    borderRadius: 5,
    color: '#000',
    padding: 10,
    margin: 40
  }
});